package com.elinshaw.netty4.config

import cn.hutool.core.date.DateUtil
import groovy.util.logging.Slf4j
import io.netty.channel.ChannelHandlerContext
import io.netty.channel.ChannelInboundHandlerAdapter

/**
 * @author Administrator
 */
@Slf4j
class ClientHandler extends ChannelInboundHandlerAdapter {

    /** 发送次数 */
    private int count = 1

    @Override
    void channelActive(ChannelHandlerContext ctx) throws Exception {
        log.info("客户端建立连接时间{}", DateUtil.now())
        ctx.fireChannelActive()
    }

    @Override
    void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        log.info("第{}次客户端接收消息{}", count, msg)
        count++
    }

    @Override
    void channelInactive(ChannelHandlerContext ctx) throws Exception {
        log.info("客户端断开时间为{}", DateUtil.now())
    }

}
